﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using Microsoft.Xna.Framework.Graphics;

namespace lok3_map_builder
{
    class MapTiles : IEnumerable<float>
    {
        public List<Tile> mMapTiles = new List<Tile>();
        private GraphicsDevice _gd;
        private SpriteBatch _sb;
        string _path;

        public MapTiles(GraphicsDevice gd, SpriteBatch sb, string path)
        {
            _gd = gd;
            _sb = sb;
            _path = path;
            
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        public IEnumerator<float> GetEnumerator()
        {
            Image iMapTiles;
            Tile tileBuffer;
            iMapTiles = Image.FromFile(_path);
            int ImageCountX = iMapTiles.Width / 100;
            int ImageCountY = iMapTiles.Height / 100;
            int count = 0;
            float progress = 0;

            for (int y = 0; y < ImageCountY; y++)
            {
                for (int x = 0; x < ImageCountX; x++)
                {
                    Bitmap bmImg = new Bitmap(100, 100, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                    Graphics grImg = Graphics.FromImage(bmImg);
                    grImg.DrawImage(iMapTiles, new System.Drawing.Rectangle(0, 0, 100, 100), x * 100, y * 100, 100, 100, System.Drawing.GraphicsUnit.Pixel);

                    tileBuffer = new Tile();
                    tileBuffer.Load(_gd, bmImg, new Microsoft.Xna.Framework.Color(41, 239, 214));

                    mMapTiles.Add(tileBuffer);

                    bmImg.Dispose();
                    grImg.Dispose();
                    yield return (float)mMapTiles.Count / (ImageCountX * ImageCountY);
                }
            }
        }
    }
}
